package com.wavehi.homework.third;

import com.wavehi.pojo.Person;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @Author WaveHi
 * @Date 25/8/2021 下午 8:01
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description Third homework : Get Object
 */
public class ReflectionDemo3 {

    public static void main(String[] args) throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException {

        //获取Class对象
        Class<Person> personClass = Person.class;

        //获取构造器
        Constructor<Person> constructor = personClass.getConstructor(String.class, int.class);

        //创建对象
        Person person1 = personClass.newInstance();
        Person tom = constructor.newInstance("Tom", 12);
        System.out.println(person1);
        System.out.println(tom);

    }

}

//小结：通过Class对象获取对象时，需保证Class对象对应类具有无参构造器；否则报异常。